<script setup name="Example1">
import {example1Add, example1Del, example1Edit, example1GetList} from "@/api/sys/example1";
import ExampleSearch from "@/views/sys/example1/search";
import ExampleSelect from "@/views/sys/example1/select";
import {getCurrentInstance, onMounted, reactive} from "vue";
import {useRoute} from "vue-router";

const {proxy} = getCurrentInstance();
const {hrp_user_sex, sys_is} = proxy.useDict('hrp_user_sex', 'sys_is');
const route = useRoute();

onMounted(() => {
  pd.queryParams = {...pd.queryParams, ...route.query};
  getList();
});

const pd = reactive({
  open: false,
  title: "",
  form: {},
  exampleList: [],
  loading: true,
  showSearch: true,
  total: 0,
  daterangeUpdate_time: [],
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    enableFlag: "0"
  },
  rules: {
    enableFlag: [{
      required: true,
      message: "是否不能为空",
      trigger: "change"
    }],
    name: [{
      required: true,
      message: "名称不能为空",
      trigger: "blur"
    }]
  }

});

/** 查询合作伙伴列表 */
function getList() {
  pd.loading = true;
  if (null != pd.daterangeUpdate_time && '' != pd.daterangeUpdate_time) {
    pd.queryParams.beginTime = pd.daterangeUpdate_time[0];
    pd.queryParams.endTime = pd.daterangeUpdate_time[1];
  }
  example1GetList(pd.queryParams).then(res => {
    pd.exampleList = res.data;
    pd.total = res.total;
    pd.loading = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  pd.queryParams.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  pd.queryParams.beginTime = null;
  pd.queryParams.endTime = null;
  pd.queryParams.name = null;
  pd.daterangeUpdate_time = [];
  proxy.resetForm("queryRef");
  handleQuery();
}


/** 修改按钮操作 */
function handleDel(id) {
  if (!id) {
    return
  }
  proxy.$modal.confirm('是否确认删除样例代码编号为:"' + id + '"的数据项？').then(function () {
    return example1Del(id);
  }).then(() => {
    proxy.$modal.msgSuccess("删除成功");
    pd.open = false;
    getList()
  }).catch(() => {
  });
}

//============================对话框处理


/** 新增按钮操作 */
function handleOpen(example) {
  resetExampleForm();
  if (example) {
    pd.form = {...pd.form, ...example};
    pd.title = "修改样例代码";
  } else {
    pd.title = "新增样例代码";
  }
  pd.open = true;
}

// 表单重置
function resetExampleForm() {
  pd.form = {
    id: null,
    name: null,
    sex: null,
    state: "0",
    enableFlag: "0",
    remark: null
  };
  pd.title = "";
  proxy.resetForm("exampleRef");
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["exampleRef"].validate(valid => {
    if (valid) {
      if (pd.form.id) {
        example1Edit(pd.form).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          pd.open = false;
          getList()
        });
      } else {
        example1Add(pd.form).then(res => {
          proxy.$modal.msgSuccess("新增成功");
          pd.open = false;
          getList()
        });
      }
    }
  });
}

</script>

<template>
  <div class="app-container">
    <el-form :model="pd.queryParams" ref="queryRef" :inline="true" v-show="pd.showSearch" label-width="68px">
      <el-form-item prop="name">
        <el-input v-model="pd.queryParams.id" placeholder="请输入搜索名称" @keyup.enter="handleQuery"/>
      </el-form-item>
      <el-form-item prop="enableFlag">
        <el-switch v-model="pd.queryParams.enableFlag" active-text="启用" inactive-text="停用"
                   active-value="0" inactive-value="1"></el-switch>
      </el-form-item>
      <el-form-item label="更新时间" style="width: 308px">
        <el-date-picker v-model="pd.daterangeUpdate_time" value-format="YYYY-MM-DD" type="daterange" range-separator="-"
                        start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="Search" @click="handleQuery">搜 索</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleOpen({})" v-hasPermi="['sys:example:add']">新 增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="Upload" v-hasPermi="['sys:example:add']">导 入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" v-hasPermi="['sys:example:export']">导 出</el-button>
      </el-col>
      <el-col :span="1.5">
        <ExampleSearch :searchType=1 :dataText="pd.queryParams.name" @ok=""/>
      </el-col>
      <el-col :span="1.5">
        <ExampleSelect v-model:code="pd.queryParams.id" @ok=""/>
      </el-col>
      <right-toolbar v-model:showSearch="pd.showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="pd.loading" :data="pd.exampleList">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="名称" align="center" prop="name">
        <template #default="scope">
          <el-link type="primary" @click="handleOpen(scope.row)" v-hasPermi="['sys:example:edit']">
            {{ scope.row.name }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center" prop="sex">
        <template #default="scope">
          <dict-tag :options="hrp_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="state">
        <template #default="scope">
          <dict-tag :options="sys_is" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="启用" align="center" prop="enableFlag">
        <template #default="scope">
          <dict-tag :options="sys_is" :value="scope.row.enableFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="创建人" align="center" prop="createBy"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" align="center" prop="updateBy"/>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="220">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleOpen(scope.row)" v-hasPermi="['sys:example:edit']">
            修改
          </el-button>
          <el-button link type="primary" icon="DataLine" v-hasPermi="['sys:example:edit']">
            审批
          </el-button>
          <el-button link type="primary" icon="More" v-hasPermi="['sys:example:edit']">
            其他
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDel(scope.row.id)"
                     v-hasPermi="['sys:example:del']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="pd.total > 0" :total="pd.total" v-model:page="pd.queryParams.pageNum"
                v-model:limit="pd.queryParams.pageSize" @pagination="getList"/>

    <!-- 添加或修改样例代码对话框 -->
    <el-dialog :title="pd.title" v-model="pd.open" width="700px" append-to-body>
      <el-form ref="exampleRef" :model="pd.form" :rules="pd.rules" label-width="80px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="名称" prop="name">
              <el-input v-model="pd.form.name" placeholder="请输入名称"/>
            </el-form-item>
            <el-form-item label="性别" prop="sex">
              <el-select v-model="pd.form.sex" placeholder="请选择性别">
                <el-option v-for="item in hrp_user_sex" :key="item.code" :label="item.name"
                           :value="item.code"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="启用" prop="enableFlag">
              <el-switch v-model="pd.form.enableFlag" active-value="0" inactive-value="1"></el-switch>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="状态" prop="state">
              <el-switch v-model="pd.form.state" active-value="0" inactive-value="1"></el-switch>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="pd.form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button v-if="!pd.form.id" type="primary" @click="submitForm">新 增</el-button>
          <el-button v-if="pd.form.id" type="success" @click="submitForm">修 改</el-button>
          <el-button v-if="pd.form.id" type="danger" @click="del(pd.form.id)">删 除</el-button>
          <el-button type="info" @click="pd.open = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<style lang='scss' scoped>

</style>